clc;
close all;
clear all

%load('D:\movement Recognition\procedure\second\greedseqdatakth4.mat');
%load('D:\movement Recognition\procedure\second\greedseqdatakth4_5M121K.mat');
%load('D:\movement Recognition\procedure\second\greedseqdatakth5_5M121K_20.mat');
load('greedseqdatakth_kun.mat');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%test=cell(6,9);
%sample=cell(6,13);
sample={rseqrun{1,1}(1,103:119) rseqrun{1,2}(1,72:88) rseqrun{1,6}(1,108:120) rseqrun{1,7}(1,106:129) rseqrun{1,9}(1,117:132) rseqrun{1,11}(1,80:96) rseqrun{1,12}(1,86:108) rseqrun{1,14}(1,115:136) rseqrun{1,15}(1,121:141) rseqrun{1,1}(1,308:324) rseqrun{1,2}(1,215:234) rseqrun{1,6}(1,324:336) rseqrun{1,7}(1,318:345) rseqrun{1,9}(1,350:368) rseqrun{1,11}(1,237:254) rseqrun{1,12}(1,317:338) rseqrun{1,14}(1,336:355) rseqrun{1,15}(1,364:383) rseqrun{1,4}(1,89:102) rseqrun{1,4}(1,289:303) rseqrun{1,13}(1,116:142) rseqrun{1,13}(1,326:354);
    rseqwalk{1,1}(1,13:61) rseqwalk{1,2}(1,18:62) rseqwalk{1,6}(1,32:92) rseqwalk{1,7}(1,13:66) rseqwalk{1,9}(1,22:60) rseqwalk{1,11}(1,18:55) rseqwalk{1,12}(1,15:66) rseqwalk{1,14}(1,154:202) rseqwalk{1,15}(1,19:62)   rseqwalk{1,1}(1,341:388) rseqwalk{1,2}(1,291:337) rseqwalk{1,6}(1,374:436) rseqwalk{1,7}(1,300:355) rseqwalk{1,9}(1,435:478) rseqwalk{1,11}(1,254:289) rseqwalk{1,12}(1,317:366) rseqwalk{1,14}(1,463:509) rseqwalk{1,15}(1,341:385) rseqwalk{1,4}(1,16:48) rseqwalk{1,4}(1,300:337) rseqwalk{1,13}(1,18:66) rseqwalk{1,13}(1,364:411);
rseqbox{1,1}(1,1:50) rseqbox{1,2}(1,1:50) rseqbox{1,6}(1,1:50) rseqbox{1,7}(1,1:50) rseqbox{1,9}(1,1:50) rseqbox{1,11}(1,1:50) rseqbox{1,12}(1,1:50) rseqbox{1,14}(1,1:50) rseqbox{1,15}(1,1:50) rseqbox{1,1}(1,51:100) rseqbox{1,2}(1,51:100) rseqbox{1,6}(1,51:100) rseqbox{1,7}(1,51:80) rseqbox{1,9}(1,51:80) rseqbox{1,11}(1,51:80) rseqbox{1,12}(1,51:80) rseqbox{1,14}(1,51:80) rseqbox{1,15}(1,51:100) rseqbox{1,4}(1,51:80) rseqbox{1,5}(1,1:50) rseqbox{1,13}(1,1:50) rseqbox{1,7}(1,101:140);    

    rseqwave{1,1}(1,1:34) rseqwave{1,2}(1,1:34) rseqwave{1,6}(1,1:34) rseqwave{1,7}(1,1:41) rseqwave{1,9}(1,1:50) rseqwave{1,11}(1,1:50) rseqwave{1,12}(1,1:50) rseqwave{1,14}(1,1:50) rseqwave{1,15}(1,1:50) rseqwave{1,1}(1,35:80) rseqwave{1,2}(1,35:80) rseqwave{1,6}(1,35:80) rseqwave{1,7}(1,42:90) rseqwave{1,9}(1,51:100) rseqwave{1,11}(1,51:100) rseqwave{1,12}(1,51:100) rseqwave{1,14}(1,51:100) rseqwave{1,15}(1,51:100) rseqwave{1,4}(1,51:100) rseqwave{1,5}(1,1:50) rseqwave{1,13}(1,1:50) rseqwave{1,7}(1,101:140);

rseqclap{1,1}(1,1:50) rseqclap{1,2}(1,1:50) rseqclap{1,6}(1,1:50) rseqclap{1,7}(1,1:50) rseqclap{1,9}(1,117:132) rseqclap{1,11}(1,1:50) rseqclap{1,12}(1,1:50) rseqclap{1,14}(1,1:50) rseqclap{1,15}(1,1:50) rseqclap{1,1}(1,51:100) rseqclap{1,2}(1,51:100) rseqclap{1,6}(1,51:100) rseqclap{1,7}(1,51:80) rseqclap{1,9}(1,51:100) rseqclap{1,11}(1,51:100) rseqclap{1,12}(1,51:80) rseqclap{1,14}(1,51:80) rseqclap{1,15}(1,51:80) rseqclap{1,4}(1,51:80) rseqclap{1,5}(1,1:50) rseqclap{1,13}(1,1:50) rseqclap{1,7}(1,101:140);

};

[m,n]=size(sample);
for i=1:n
    if i==1
        
         ee={sample{1,2:end}};
    elseif i==n
        ee={sample{1,1:end-1}};
    else
        ee={sample{1,1:i-1} sample{i+1,end}};
    end
   % ee{i}={}
   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %选择样本
 %samplecell={sample{1,:}};
%testcell={test{1,:}};
% O：观察状态数
    O =121;
% Q：HMM状态数
    Q =5;
%训练的数据集,每一行数据就是一组训练的观察值
%datarun=[rseqrun{1,1}(1,8:26)  rseqrun{1,2}(1,9:27) rseqrun{1,4}(z4) rseqrun{1,5}(z5)];
%datahmm=cell2mat(samplecell);

        
    prior0 = normalise(rand(Q,1));
    transmat0 = mk_stochastic(rand(Q,Q));
    obsmat0 = mk_stochastic(rand(Q,O));
    
    [LL, prior1, transmat1, obsmat1] = dhmm_em(ee, prior0, transmat0, obsmat0, 'max_iter',250);
%datawalk

    [LL, prior2, transmat2, obsmat2] = dhmm_em({sample{2,:}}, prior0, transmat0, obsmat0, 'max_iter',500);
%databox
    [LL, prior3, transmat3, obsmat3] = dhmm_em({sample{3,:}}, prior0, transmat0, obsmat0, 'max_iter',500);
%datawave
    [LL, prior4, transmat4, obsmat4] = dhmm_em({sample{4,:}}, prior0, transmat0, obsmat0, 'max_iter',500);
%dataclap
    [LL, prior5, transmat5, obsmat5] = dhmm_em({sample{5,:}}, prior0, transmat0, obsmat0, 'max_iter',500);
    datatest=sample{1,i}(1,:);%rseqwalk{1,1}(1,341:388)
    loglik(1) = dhmm_logprob(datatest, prior1, transmat1, obsmat1);
    loglik(2) = dhmm_logprob(datatest, prior2, transmat2, obsmat2);
    loglik(3) = dhmm_logprob(datatest, prior3, transmat3, obsmat3);
    loglik(4) = dhmm_logprob(datatest, prior4, transmat4, obsmat4);
    loglik(5) = dhmm_logprob(datatest, prior5, transmat5, obsmat5);
    [ee,ind]=max(loglik);
    testresult(i)=ind;
end
precision=sum(testresult==1)/n     
testresult

